from django.db import models
from django.contrib.auth.models import User

status_choices = (
    (1, 'New'),
    (2, 'In progress'),
    (3, 'Accepted'),
    (4, 'Approved'),
    (5, 'Rejected'),
    (6, 'Closed'),
)

workorder_status_choices = (
    (1, 'New'),
    (2, 'In progress'),
    (3, 'Rejected'),
    (4, 'Closed'),
)

priority_choices = (
    (1, 'Low'),
    (2, 'Normal'),
    (3, 'High'),
)

workorder_type_choices = (
    (1, 'WorkOrder Type 1'),
    (2, 'WorkOrder Type 2'),
    (3, 'WorkOrder Type 3'),
    (4, 'WorkOrder Type 4'),
    (5, 'WorkOrder Type 5'),
)

class Service(models.Model):
    service = models.CharField(max_length=255)
    
    def __unicode__(self):
        return self.service


class Infrastructure(models.Model):
    infrastructure = models.CharField(max_length=255)
    
    def __unicode__(self):
        return self.infrastructure


class RFC(models.Model):
    summary = models.CharField(max_length=255)
    description = models.TextField()
    services = models.ManyToManyField(Service, blank=True, null=True)
    infrastructure = models.ManyToManyField(Infrastructure, blank=True, null=True)
    status = models.IntegerField(choices=status_choices, blank=True)
    owner = models.ForeignKey(User,related_name='owner', blank=True, null=True)
    initiator = models.ForeignKey(User,related_name='initiator', editable=False)
    priority = models.IntegerField(choices=priority_choices, blank=True, null=True)
    updateUser = models.ForeignKey(User,related_name='rfcUpdateUser', blank=True, null=True, editable=False)
    createDate = models.DateTimeField(auto_now=False, auto_now_add=True, editable=False)
    updateDate = models.DateTimeField(auto_now=True, auto_now_add=True, editable=False)
    lockTime = models.DateTimeField(blank=True, editable=False, null=True)
    lockUser = models.ForeignKey(User,related_name='rfcLockUser', editable=False, blank=True, null=True)
    editable = models.BooleanField(blank=True, editable=False)
    
    def __unicode__(self):
        return self.summary

class WorkOrder(models.Model):
    rfc = models.ForeignKey(RFC, related_name='rfc')
    plannedStart = models.DateField(blank=True, null=True)
    plannedEnd = models.DateField(blank=True, null=True)
    actualStart = models.DateField(blank=True, null=True)
    actualEnd = models.DateField(blank=True, null=True)
    workOrderType = models.IntegerField(blank=True, null=True, choices=workorder_type_choices)
    responsible = models.ForeignKey(User,related_name='responsible', blank=True, null=True)
    description = models.TextField()
    affectedServices = models.ManyToManyField(Service, blank=True, null=True)
    affectedInfrastructure = models.ManyToManyField(Infrastructure, blank=True, null=True)
    plannedEffort = models.IntegerField(blank=True, null=True)
    status = models.IntegerField(choices=workorder_status_choices)
    updateUser = models.ForeignKey(User,related_name='woUpdateUser', blank=True, null=True, editable=False)
    createDate = models.DateTimeField(auto_now=False, auto_now_add=True, editable=False)
    updateDate = models.DateTimeField(auto_now=True, auto_now_add=True, editable=False)
    lockTime = models.DateTimeField(blank=True, editable=False, null=True)
    lockUser = models.ForeignKey(User,related_name='woLockUser', editable=False, blank=True, null=True)
    editable = models.BooleanField(blank=True, editable=False)
    
    def __unicode__(self):
        return self.description
